package com.qf.oa_system.service.impl;

import com.qf.oa_system.entity.Roles;
import com.qf.oa_system.entity.User;
import com.qf.oa_system.entity.UserRoles;
import com.qf.oa_system.mapper.RolesDao;
import com.qf.oa_system.mapper.UserDao;
import com.qf.oa_system.mapper.UserRolesDao;
import com.qf.oa_system.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2020-06-03 20:26:32
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    @Resource
    private RolesDao rolesDao;

    @Resource
    private UserRolesDao userRolesDao;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer userId) {
        return this.userDao.queryById(userId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<User> queryAllByLimit(int offset, int limit) {
        return this.userDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.userDao.deleteById(userId) > 0;
    }

    @Override
    public User queryByName(String userName) {
        return this.userDao.queryByName(userName);
    }

    @Override
    public List<User> queryAll() {
        List<User> users = userDao.queryAll2();
        for (User user:users){
            UserRoles userRoles = userRolesDao.queryById(user.getUserId());
            Roles roles = rolesDao.queryById(userRoles.getRoleId());
            user.setRoles(roles);
        }
        return users;
    }

    @Override
    public void setimg(String image,int userId) {
        userDao.setimg(image,userId);
    }
}